package com.tarena.baking.common.ex;

import com.tarena.baking.response.JsonResult;
import com.tarena.baking.response.StatusCode;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 功能描述: controller层的BindException被捕获统一处理为1002, "参数原因"
     */
    @ExceptionHandler(BindException.class)
    public JsonResult BindExceptionHandler(BindException e) {
        // 从异常对象中拿到ObjectError对象
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        log.warn("参数校验失败{}",objectError.getDefaultMessage());
        return JsonResult.fail(StatusCode.ERROR_VALIDATE,"参数校验失败");
    }

    /**
     * 功能描述: controller层的针对@RequestBody报出的异常拦截处理
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        // 从异常对象中拿到ObjectError对象
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        log.warn("参数校验失败{}",objectError.getDefaultMessage());
        return JsonResult.fail(StatusCode.ERROR_VALIDATE,"参数校验失败");
    }

    /**
     * 用户名或密码错误时抛出的异常
     */
    @ExceptionHandler({InternalAuthenticationServiceException.class, BadCredentialsException.class})
    public JsonResult handleAuthenticationException(AuthenticationException e) {

        log.warn("异常信息：{}", e.getMessage());
        if (e instanceof InternalAuthenticationServiceException) {
            log.warn("用户名不存在");
            return JsonResult.fail(StatusCode.USER_NOT_EXISTS, "用户名不存在");
        }
        //就是密码错误
        log.warn("密码错误");
        return JsonResult.fail(StatusCode.USER_PASSWORD_ERROR, "密码错误");
    }



    /**
     * 自定义异常捕获
     */
    @ExceptionHandler(ServiceException.class)
    public JsonResult APIExceptionHandler(ServiceException e) {
        return JsonResult.fail(e.getStatusCode(), e.getMessage());
    }

    /**
     * 功能描述: 账号被禁用
     */
    @ExceptionHandler(DisabledException.class)
    public JsonResult AuthenticationExceptionHandler(DisabledException e) {
        return JsonResult.fail(StatusCode.ERROR_UNAUTHORIZED_DISABLED,"账号被禁用");
    }

    /**
     * 无访问权限时抛出的异常
     */
    @ExceptionHandler(AccessDeniedException.class)
    public JsonResult AuthenticationExceptionHandler(AccessDeniedException e) {
        System.out.println("无权访问!");
        return JsonResult.fail(StatusCode.ERROR_FORBIDDEN,"无权访问");
    }

}
